﻿using Authentication.Core.Configuration;
using Authentication.Model.Interface;
using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Data.Repository.Enum;
using Common.Service.Command;
using System;

namespace Authentication.Core.ServiceCommand
{
    public class UpdateStatusEditDaoServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateEditDaoRequestRequest, ResponseRecord>
    {
        public UpdateStatusEditDaoServiceCommand(IProviderFactory providers) : base(providers) { }
        #region Private Properties
        private UserRecord currentUser;
        #endregion
        protected override void Validate(UpdateEditDaoRequestRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("User record must be set", "request");
            }
            else
            {
                currentUser = request.CurrentUser;
            }
            if (request.Record == null || request.Record.Id == 0 || request.Record.Status == 0)
            {
                throw new ArgumentException("Record must be set", "request");
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var record = Request.Record;
            bool isPermission = false;
            bool savedUser = false;
            try
            {
                var checkUser = Providers.EditDao.RetrieveRequestByRequestId(record.Id, Request.CurrentUser);
                if (record.Status == (int)StatusCloseDaoEnum.Reject && (currentUser.GroupIdentifier.Equals("4") || currentUser.GroupIdentifier.Equals("6")))
                {
                    if (checkUser != null && checkUser.Pic.Equals(currentUser.UserName))
                    {
                        isPermission = true;
                    }
                }

                if (currentUser.GroupIdentifier.Equals("3") || currentUser.GroupIdentifier.Equals("2") || SystemConfiguration.UserMBDApprovedDao.Contains(currentUser.UserName) || SystemConfiguration.UserBICApprovedDao.Contains(currentUser.UserName))
                {
                    isPermission = true;
                }

                if (isPermission)
                {
                    if (checkUser != null)
                    {
                        if (record.Status == (int)StatusCloseDaoEnum.Approve)
                        {
                            switch (currentUser.GroupIdentifier)
                            {
                                case "3":
                                    checkUser.Status = (int)StatusCloseDaoEnum.BranchApproved;
                                    break;
                                case "2":
                                    checkUser.Status = (int)StatusCloseDaoEnum.RegionApproved;
                                    break;
                                case "1":
                                    if (SystemConfiguration.UserMBDApprovedDao.Contains(currentUser.UserName))
                                    {
                                        checkUser.Status = (int)StatusCloseDaoEnum.RegionApproved;
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                        else
                        {
                            checkUser.Status = record.Status;
                        }
                        if (record.Status == (int)StatusRequestCreateDaoEnum.Reject)
                        {
                            checkUser.RejectPerson = currentUser.UserName;
                            checkUser.GroupIdReject = int.Parse(currentUser.GroupIdentifier);
                            checkUser.RejectReason = string.IsNullOrEmpty(record.RejectReason) ? string.Empty : record.RejectReason;
                        }
                        savedUser = Providers.EditDao.UpdateStatusEditDaoRequest(checkUser);
                    }
                    else
                    {
                        response.ErrorMessage = string.Format("Request is invalid!");
                    }
                }
                else
                {
                    response.ErrorMessage = string.Format("User {0} not have permission", currentUser.UserName);
                }
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
            }
            return savedUser;
        }
    }
}
